package com.ceac.test;

import com.ceac.kronos.ejb.entitybeans.Category;
import com.ceac.kronos.ejb.entitybeans.Checkpoint;
import com.ceac.kronos.ejb.entitybeans.Competition;
import com.ceac.kronos.ejb.entitybeans.Edition;
import com.ceac.kronos.ejb.entitybeans.Participant;
import com.ceac.kronos.ejb.entitybeans.Person;
import com.ceac.kronos.ejb.entitybeans.Race;
import com.ceac.kronos.ejb.entitybeans.Role;
import com.ceac.kronos.ejb.entitybeans.Time;
import com.ceac.kronos.ejb.sessionbeans.KronosSessionEJB;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class KronosSessionEJBClient {
    public static void main(String [] args) {
        try {
            final Context context = getInitialContext();
            KronosSessionEJB kronosSessionEJB = (KronosSessionEJB)context.lookup("CEAC-Kronos-KronosSessionEJB#com.ceac.kronos.ejb.sessionbeans.KronosSessionEJB");
            
            System.out.println(kronosSessionEJB);

            kronosSessionEJB.createCompetition(
                "VHO", 
                "Vallhonesta", 
                "Cursa de muntanya de Vallhonesta", 
                1, 
                new GregorianCalendar(2011, Calendar.SEPTEMBER, 23).getTime(),
                "Cursa Senior",
                13800, 
                1, 
                99999
            );
            System.exit(0);
                
            Competition myCompetition = kronosSessionEJB.getCompetitionFindByName("Milla Nocturna de Sant Vicenç de Castellet");

            printCompetition(myCompetition);
            Edition myEdition = kronosSessionEJB.getEditionFindByName(myCompetition, 8);
            printEdition(myEdition);
            Race myRace = kronosSessionEJB.getRaceFindByName(myEdition, "VETERANS");
            printRace(myRace);
            Person thePerson = kronosSessionEJB.getPersonFindByIdCard("40446819P");
            printPerson(thePerson);
            List<Participant> participations = kronosSessionEJB.getParticipantFindParticipation(thePerson, myRace);
            System.out.println(participations.size()>0);
            System.out.println(participations.size());
            if(participations.size()>0) {
                kronosSessionEJB.removeParticipant(participations.get(0));
            }
            else {
                Participant newParticipant = new Participant();
                newParticipant.setPerson(thePerson);
                newParticipant.setRace(myRace);
                newParticipant.setBibsNumber(45);
                newParticipant = kronosSessionEJB.persistParticipant(newParticipant);
                System.out.println(newParticipant.getParticipantId());
            }
            System.exit(1);
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void printTime(Time time) {
        System.out.println( "createdBy = " + time.getCreatedBy() );
        System.out.println( "creationDate = " + time.getCreationDate() );
        System.out.println( "lastUpdatedBy = " + time.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + time.getLastUpdateDate() );
        System.out.println( "timeId = " + time.getTimeId() );
        System.out.println( "timeStamp = " + time.getTimeStamp() );
        System.out.println( "participant = " + time.getParticipant() );
        System.out.println( "checkpoint = " + time.getCheckpoint() );
    }

    private static void printEdition(Edition edition) {
        System.out.println( "alternativeName = " + edition.getAlternativeName() );
        System.out.println( "createdBy = " + edition.getCreatedBy() );
        System.out.println( "creationDate = " + edition.getCreationDate() );
        System.out.println( "editionId = " + edition.getEditionId() );
        System.out.println( "editionNumber = " + edition.getEditionNumber() );
        System.out.println( "eventDate = " + edition.getEventDate() );
        System.out.println( "lastUpdatedBy = " + edition.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + edition.getLastUpdateDate() );
        System.out.println( "races = " + edition.getRaces() );
        System.out.println( "competition = " + edition.getCompetition() );
    }

    private static void printCompetition(Competition competition) {
        System.out.println( "city = " + competition.getCity() );
        System.out.println( "competitionId = " + competition.getCompetitionId() );
        System.out.println( "county = " + competition.getCounty() );
        System.out.println( "createdBy = " + competition.getCreatedBy() );
        System.out.println( "creationDate = " + competition.getCreationDate() );
        System.out.println( "description = " + competition.getDescription() );
        System.out.println( "firstEditionDate = " + competition.getFirstEditionDate() );
        System.out.println( "lastUpdatedBy = " + competition.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + competition.getLastUpdateDate() );
        System.out.println( "name = " + competition.getName() );
        System.out.println( "organizedBy = " + competition.getOrganizedBy() );
        System.out.println( "postalCode = " + competition.getPostalCode() );
        System.out.println( "province = " + competition.getProvince() );
        System.out.println( "editions = " + competition.getEditions() );
    }

    private static void printCategory(Category category) {
        System.out.println( "categoryId = " + category.getCategoryId() );
        System.out.println( "createdBy = " + category.getCreatedBy() );
        System.out.println( "creationDate = " + category.getCreationDate() );
        System.out.println( "description = " + category.getDescription() );
        System.out.println( "lastUpdatedBy = " + category.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + category.getLastUpdateDate() );
        System.out.println( "name = " + category.getName() );
        System.out.println( "rangeFrom = " + category.getRangeFrom() );
        System.out.println( "rangeTo = " + category.getRangeTo() );
    }

    private static void printParticipant(Participant participant) {
        System.out.println( "bibsNumber = " + participant.getBibsNumber() );
        System.out.println( "createdBy = " + participant.getCreatedBy() );
        System.out.println( "creationDate = " + participant.getCreationDate() );
        System.out.println( "finishTime = " + participant.getFinishTime() );
        System.out.println( "lastUpdatedBy = " + participant.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + participant.getLastUpdateDate() );
        System.out.println( "participantId = " + participant.getParticipantId() );
        System.out.println( "person = " + participant.getPerson() );
        System.out.println( "race = " + participant.getRace() );
        System.out.println( "times = " + participant.getTimes() );
    }

    private static void printRace(Race race) {
        System.out.println( "actualStartTime = " + race.getActualStartTime() );
        System.out.println( "createdBy = " + race.getCreatedBy() );
        System.out.println( "creationDate = " + race.getCreationDate() );
        System.out.println( "description = " + race.getDescription() );
        System.out.println( "distance = " + race.getDistance() );
        System.out.println( "endTime = " + race.getEndTime() );
        System.out.println( "laps = " + race.getLaps() );
        System.out.println( "lapDistance = " + race.getLapDistance() );
        System.out.println( "lastUpdatedBy = " + race.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + race.getLastUpdateDate() );
        System.out.println( "maxParticipants = " + race.getMaxParticipants() );
        System.out.println( "raceId = " + race.getRaceId() );
        System.out.println( "startTime = " + race.getStartTime() );
        System.out.println( "surface = " + race.getSurface() );
        System.out.println( "participants = " + race.getParticipants() );
        System.out.println( "checkpoints = " + race.getCheckpoints() );
        System.out.println( "edition = " + race.getEdition() );
    }

    private static void printCheckpoint(Checkpoint checkpoint) {
        System.out.println( "checkpointId = " + checkpoint.getCheckpointId() );
        System.out.println( "createdBy = " + checkpoint.getCreatedBy() );
        System.out.println( "creationDate = " + checkpoint.getCreationDate() );
        System.out.println( "description = " + checkpoint.getDescription() );
        System.out.println( "distance = " + checkpoint.getDistance() );
        System.out.println( "finishLine = " + checkpoint.getFinishLine() );
        System.out.println( "lastUpdatedBy = " + checkpoint.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + checkpoint.getLastUpdateDate() );
        System.out.println( "name = " + checkpoint.getName() );
        System.out.println( "race = " + checkpoint.getRace() );
        System.out.println( "times = " + checkpoint.getTimes() );
    }

    private static void printPerson(Person person) {
        System.out.println( "address = " + person.getAddress() );
        System.out.println( "alias = " + person.getAlias() );
        System.out.println( "birthDate = " + person.getBirthDate() );
        System.out.println( "cellPhone = " + person.getCellPhone() );
        System.out.println( "city = " + person.getCity() );
        System.out.println( "comments = " + person.getComments() );
        System.out.println( "county = " + person.getCounty() );
        System.out.println( "createdBy = " + person.getCreatedBy() );
        System.out.println( "creationDate = " + person.getCreationDate() );
        System.out.println( "emailAddress = " + person.getEmailAddress() );
        System.out.println( "endActiveDate = " + person.getEndActiveDate() );
        System.out.println( "fax = " + person.getFax() );
        System.out.println( "idCard = " + person.getIdCard() );
        System.out.println( "isoCountry = " + person.getIsoCountry() );
        System.out.println( "lastUpdatedBy = " + person.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + person.getLastUpdateDate() );
        System.out.println( "middleName = " + person.getMiddleName() );
        System.out.println( "name = " + person.getName() );
        System.out.println( "personId = " + person.getPersonId() );
        System.out.println( "phoneNumber = " + person.getPhoneNumber() );
        System.out.println( "postalCode = " + person.getPostalCode() );
        System.out.println( "province = " + person.getProvince() );
        System.out.println( "startActiveDate = " + person.getStartActiveDate() );
        System.out.println( "surname = " + person.getSurname() );
        System.out.println( "title = " + person.getTitle() );
        System.out.println( "participations = " + person.getParticipations() );
    }

    private static void printRole(Role role) {
        System.out.println( "createdBy = " + role.getCreatedBy() );
        System.out.println( "creationDate = " + role.getCreationDate() );
        System.out.println( "description = " + role.getDescription() );
        System.out.println( "lastUpdatedBy = " + role.getLastUpdatedBy() );
        System.out.println( "lastUpdateDate = " + role.getLastUpdateDate() );
        System.out.println( "roleId = " + role.getRoleId() );
        System.out.println( "roleName = " + role.getRoleName() );
        System.out.println( "userRoleName = " + role.getUserRoleName() );
    }

    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x connection details
        env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
        return new InitialContext( env );
    }
}
